package cn.ecut.dzy.web;


import cn.ecut.dzy.pojo.Cart;
import cn.ecut.dzy.pojo.Order;
import cn.ecut.dzy.pojo.OrderItem;
import cn.ecut.dzy.pojo.User;
import cn.ecut.dzy.service.OrderService;
import cn.ecut.dzy.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 先获取Cart购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        // 获取Userid
        User loginUser = (User) req.getSession().getAttribute("user");

        if (loginUser == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }

//        Integer userId = loginUser.getId();
////        调用orderService.createOrder(Cart,Userid);生成订单
//        String orderId = orderService.createOrder(cart, userId);
////        req.setAttribute("orderId", orderId);
//        // 请求转发到/pages/cart/checkout.jsp
////        req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
//        req.getSession().setAttribute("orderId",orderId);
//       // resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
//
//


        //调用service
        if(cart != null){

            String orderId1 = orderService.createOrder(cart, loginUser.getId());
            //回传数据
            req.getSession().setAttribute("orderId",orderId1);

            /*
             返回页面
             这个地方要使用重定向 否则在用户刷新的时候又会生成一个订单
             */
//            resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
            //跳转到支付页面
            resp.sendRedirect(req.getContextPath() + "/pages/pay/pay.jsp");
        }
    }

    /**
     * 查询所有订单
     * @author dzy
     * @date 2021-08-22 21:04
     * @param req
     * @param resp
     * @return void
     */
    protected void selectAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> orderList=orderService.queryOrders();
        //3 保存到图书到Request域中
        req.setAttribute("orderList", orderList) ;
        //4 请求转发到。pages/manager/book_edit.jsp页面
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req,resp);

    }

    /**
     * 发货
     * @author dzy
     * @date 2021-08-22 21:04
     * @param req
     * @param resp
     * @return void
     */
    protected void sendOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取订单号
        String orderId = req.getParameter("orderId");

        //发货
        orderService.sendOrder(orderId);

        //查新最新数据
        List<Order> allOrder = orderService.showAllOrders();

        //回传数据
        req.setAttribute("orderList", allOrder);

        //返回页面
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
    }
    /**
     * 查看订单详情
     * @author dzy
     * @date 2021-08-22 21:04
     * @param req
     * @param resp
     * @return void
     */
    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数 订单号
        String orderId = req.getParameter("orderId");

        //查看
        List<OrderItem> orderItemList = orderService.showOrderDetail(orderId);

        //回传数据
        req.setAttribute("orderItemList", orderItemList);

        //返回页面
        req.getRequestDispatcher("/pages/order/order_detail.jsp").forward(req, resp);
    }
    /**
     * 查看我的订单
     * @author dzy
     * @date 2021-08-22 21:04
     * @param req
     * @param resp
     * @return void
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取Userid
        User loginUser = (User) req.getSession().getAttribute("user");

        if (loginUser == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }
        List<Order> orders = orderService.queryOrdersByUserId(loginUser.getId() + "");
        //3 保存到图书到Request域中
        req.setAttribute("orderList", orders) ;
        //4 请求转发到。pages/manager/book_edit.jsp页面
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req,resp);

    }
    /**
     * 签收订单、确认收货
     * @author dzy
     * @date 2021-08-22 21:04
     * @param req
     * @param resp
     * @return void
     */
    protected void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取订单号
        String orderId = req.getParameter("orderId");

        //获取用户
        User user= (User) req.getSession().getAttribute("user");

        //签收订单
        orderService.signOrder(orderId);

        //查看
        List<Order> orderList = orderService.queryOrdersByUserId(user.getId()+"");

        //回传数据
        req.setAttribute("orderList", orderList);

        //返回页面
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req, resp);

    }

    /**
     * 支付
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void payOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取订单号
        String orderId = req.getParameter("orderId");

        //支付
        orderService.payOrder(orderId);

        //获取购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if(cart != null) {
            //支付完的东西清空购物车
            req.getSession().removeAttribute("cart");
        }

        //回传数据
        req.getSession().setAttribute("orderId", orderId);

        //返回页面
        req.getRequestDispatcher("/pages/pay/checkout.jsp").forward(req, resp);

    }

}
